import Vue from 'vue'
import Vuex from 'vuex'
import vuexpersist from 'vuex-persist'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    shopdetail:[]
  },
  mutations: {
    add(state,item){
      var flag = false
      state.shopdetail.forEach(element => {
        if(element.id == item.id){
          flag = true
          element.num++
        }
      });

      if(!flag){
        item.num=1
        state.shopdetail.push(item)
      }
    }
  },
  actions: {
  },
  getters:{
    allprice(state){
      var sum = 0
      state.shopdetail.forEach(item => {
        sum+= item.num * item.basicInfo.originalPrice
      });
      return sum
    }
  },
  plugins:[
    new vuexpersist({
      storage:window.localStorage
    }).plugin
  ]
})
